/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.odhixon.volarme.gui;

import com.odhixon.volarme.VolarME;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;

/**
 * OpcionesForm.
 * @author Ariel Aguirre <odhixon@gmail.com>
 */
public class OpcionesForm extends Form implements ActionListener {

    static public OpcionesForm instance = null;

    static public OpcionesForm getInstance() {
        if (instance == null) {
            instance = new OpcionesForm();
        }
        return instance;
    }
    private VolarME midlet;
    private List opcionesList;
    private ObjectsModel opcionesModel;
    private Command salirCommand;

    public void setMidlet(VolarME midlet) {
        this.midlet = midlet;
    }

    public OpcionesForm() {
        super();
        this.setTitle("VolarME");
        this.setLayout(new BorderLayout());
        final String[] items = {"Registros", "Nuevo registro", "Acerca de..."};
        opcionesModel = new ObjectsModel() {

            public void load() {
                objects = items;
            }
        };
        opcionesModel.load();
        opcionesList = new List(opcionesModel);
        opcionesList.setListCellRenderer(new PointerRender());
        opcionesList.addActionListener(this);
        this.addComponent(BorderLayout.CENTER, opcionesList);
        salirCommand = new Command("Salir") {

            public void actionPerformed(ActionEvent actionEvent) {
                midlet.destroyApp(true);
                midlet.notifyDestroyed();
            }
        };
        this.addCommand(salirCommand);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        int index = opcionesModel.getSelectedIndex();
        switch (index) {
            case 0:
                RegistrosForm.getInstance().show();
                break;
            case 1:
                RegistroForm registroForm = RegistroForm.getInstance();
                registroForm.setBackForm(this);
                registroForm.setRegistro(null);
                registroForm.show();
                break;
            case 2:
                AcercaDeForm.getInstance().show();
                break;
        }
    }
}
